package com.sys.data;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class sqlStruct {
    private Object value;
    private int type;

    sqlStruct(Object value, int type) {
        if (value instanceof Map) {
            JSONObject valueJson = JSONObject.fromObject(value);
            value = valueJson.toString();
        } else if (value instanceof List) {
            JSONArray valueArr = JSONArray.fromObject(value);
            value = valueArr.toString();
        }
        this.value = value;
        this.type = type;
    }

    public Object getValue() {
        return this.value;
    }

    public int getType() {
        return this.type;
    }

    @Override
    public String toString() {
        String str = "value: " + this.value.toString() + ", type: " + this.type;

        if (this.value instanceof String) {
            str = "value: " + "\"" + this.value.toString() + "\"" + ", type: " + this.type;
        } else if (this.value instanceof Date) {
            Date date = (Date) this.value;
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            str = "value: " + "\"" + df.format(date) + "\"" + ", type: " + this.type;
        } else if (this.value instanceof Map) {
            JSONObject valueJson = JSONObject.fromObject(this.value);
            str = "value: " + valueJson.toString() + ", type: " + this.type;
        } else if (this.value instanceof List) {
            JSONArray valueArr = JSONArray.fromObject(this.value);
            str = "value: " + valueArr.toString() + ", type: " + this.type;
        }
        return str;
    }
}

